import { customRequest } from "../utils/request";
import { AuthUser, IUser } from "../store/user";

export interface LoginDto {
  username: string;
  password: string;
}
export const login = async (data: LoginDto) => {
  return await customRequest<AuthUser>({
    method: "post",
    url: "/user/login",
    data,
  });
};

export interface RegisterDto {
  username: string;
  password: string;
  nickName: string;
  email: string;
  captcha: string;
}
export const register = async (data: RegisterDto) => {
  return await customRequest<IUser>({
    method: "post",
    url: "/user/register",
    data,
  });
};

// 发送注册验证码
export interface CaptchaDto {
  email: string;
}
export const registerCaptcha = async (data: CaptchaDto) => {
  return await customRequest<string>({
    method: "get",
    url: "/user/register_captcha",
    params: data,
  });
};

export const updatePasswordCaptcha = async (data: CaptchaDto) => {
  return await customRequest<string>({
    method: "get",
    url: "/user/update_password_captcha",
    params: data,
  });
};

// 更新用户密码
export interface UpdatePasswordDto {
  username: string;
  email: string;
  password: string;
  captcha: string;
}
export const updatePassword = async (data: UpdatePasswordDto) => {
  return await customRequest<string>({
    method: "post",
    url: "/user/update_password",
    data,
  });
};

// 更新用户信息
export interface UpdateUserDto {
  nickName: string;
  avatar: string;
}
export const updateUserInfo = async (data: UpdateUserDto) => {
  return await customRequest<string>({
    method: "post",
    url: "/user/update",
    data,
  });
};

// 获取用户信息
export const getUserInfo = async () => {
  return await customRequest<IUser>({
    method: "get",
    url: "/user/info",
  });
};
